package com.example.wangning.mycarofcountry.find;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;
import com.example.wangning.mycarofcountry.findcar.brand.FindHotCarHotseries;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class FindRecyclerViewAdapter extends RecyclerView.Adapter<FindRecyclerViewAdapter.OtherViewHolder>{
    private List<FindHotCarHotseries> data;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private Context context;

    public FindRecyclerViewAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
        volleySingleton = VolleySingleton.getVolleyIntance();
        imageLoader = volleySingleton.getImageLoader();
    }
    public void addData(List<FindHotCarHotseries> datas){
        data = datas;
        notifyDataSetChanged();
    }
    @Override
    public OtherViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_find_item,null);
        return new OtherViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OtherViewHolder holder, int position) {
        if (data != null && data.size() > 0){
            FindHotCarHotseries hotseries = data.get(position);
            holder.seriesname.setText(hotseries.getSeriesname());
            holder.price.setText(hotseries.getPrice());
            holder.ordercount.setText("已有" + hotseries.getOrdercount() + "人订购");
            if (hotseries.getImg() != null && hotseries.getImg().length() > 0 ){
                String url = hotseries.getImg();
                volleySingleton.getBitmap(volleySingleton,url,holder.img);
            }
        }

    }

    @Override
    public int getItemCount() {
        return data.size() > 0 ? data.size() : 0;
    }

    class OtherViewHolder extends RecyclerView.ViewHolder{
        TextView seriesname,price,ordercount;
        ImageView img;

        public OtherViewHolder(View itemView) {
            super(itemView);
            seriesname = (TextView) itemView.findViewById(R.id.seriesnameTv);
            price = (TextView) itemView.findViewById(R.id.priceTv);
            ordercount = (TextView) itemView.findViewById(R.id.ordercountTv);
            img = (ImageView) itemView.findViewById(R.id.imageView);
        }
    }
}
